/*
 * Powered By xTOOLs
 * Dev by DuRisk@pactera.com
 * Copyright © 2009 - 2017 pactera.com. All rights reserved.
 *
 * Generated at 2017年12月01日 14:17:53
 *
 */

package com.pactera.jep.service.sys.web.controller;

import com.pactera.jep.core.exception.ServiceException;
import com.pactera.jep.orm.Filter;
import com.pactera.jep.orm.Order;
import com.pactera.jep.orm.Page;
import com.pactera.jep.sys.model.DashLayoutMap;
import com.pactera.jep.sys.model.DashLayoutRoleGroupMap;
import com.pactera.jep.sys.model.DashLayoutRoleMap;
import com.pactera.jep.sys.service.DashLayoutMapService;
import com.pactera.jep.sys.service.IDashLayoutRoleGroupMapService;
import com.pactera.jep.sys.service.IDashLayoutRoleMapService;
import com.pactera.jep.web.code.WebCode;
import com.pactera.jep.web.controller.CrudController;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dashLayoutMap")
public class DashLayoutMapRestController extends CrudController<DashLayoutMap> {
    private static Logger logger = LoggerFactory.getLogger(DashLayoutMapRestController.class);

    @Autowired
    private DashLayoutMapService dashLayoutMapService;

    @Autowired
    private IDashLayoutRoleGroupMapService dashLayoutRoleGroupMapService;

    @Autowired
    private IDashLayoutRoleMapService dashLayoutRoleMapService;

    /**
     * 为列表页面做数据准备
     */
    @Override
    protected void prepareQuery(Page<DashLayoutMap> page, List<Filter> filters, List<Order> orders, HttpServletRequest request) {
        dashLayoutMapService.query(page, filters, orders);
    }

    /**
     * 保存
     *
     * @param dashLayoutMap
     * @return
     */
    @ApiOperation("保存")
    @PostMapping(value = "/save")
    public Map<String, Object> save(@RequestBody DashLayoutMap dashLayoutMap) {

        Map<String, Object> tips = new HashMap<String, Object>();

        Boolean isExist = dashLayoutMapService.exists(dashLayoutMap.getMapId());
        try {
            if (isExist) {

                dashLayoutMapService.update(dashLayoutMap);
            } else {
                dashLayoutMapService.insert(dashLayoutMap);
            }

            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
        } catch (ServiceException e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_SAVE);
        }
        return tips;
    }

    /**
     * 删除
     *
     * @param mapId
     * @return
     */
    @ApiOperation("删除")
    @RequestMapping(value = "/remove/{mapId}")
    public Map<String, Object> remove(@PathVariable String mapId) {

        Map<String, Object> tips = new HashMap<String, Object>();

        try {
            dashLayoutMapService.deleteByPrimaryKey(mapId);
            tips.put(WebCode.SUCCESS, true);
            tips.put(WebCode.MSG, WebCode.SUCCESS_DELETE);
        } catch (ServiceException e) {
            logger.info(e.getMessage());
            tips.put(WebCode.SUCCESS, false);
            tips.put(WebCode.MSG, WebCode.FAILURE_DELETE);
        }

        return tips;
    }

    /**
     * 验证提交的mapId是否已经存在数据库中
     *
     * @param mapId
     * @return
     */
    @RequestMapping(value = "/check", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> check(String mapId) {

        Map<String, Object> tips = new HashMap<String, Object>();

        boolean exist = dashLayoutMapService.exists(mapId);
        tips.put("valid", !exist);

        return tips;
    }

    /**
     * 分页查询角色组授权数据
     *
     * @param page
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/searchDashLayoutRoleGroupMap")
    @ResponseBody
    public Map<String, Object> searchDashLayoutRoleGroupMap(Page<DashLayoutRoleGroupMap> page, Model model, HttpServletRequest request, DashLayoutRoleGroupMap dashLayoutRoleGroupMap) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<DashLayoutRoleGroupMap> pages = dashLayoutRoleGroupMapService.searchDashLayoutRoleGroupMap(page, dashLayoutRoleGroupMap);
        map.put(RESULT_TOTAL, pages.getTotal());
        map.put(RESULT_ROWS, pages.getResult());
        return map;
    }


    /**
     * 角色组授权设置界面数据保存
     *
     * @param dashLayoutRoleGroupMap
     * @return
     */
    @ApiOperation("角色组授权设置界面数据保存")
    @RequestMapping("/roleGroupSave")
    public Map<String, Object> roleGroupSave(@RequestBody DashLayoutRoleGroupMap dashLayoutRoleGroupMap) {
        Map<String, Object> data = new HashMap<String, Object>();
        try {
            dashLayoutRoleGroupMapService.saveDashLayoutRoleGroupMap(dashLayoutRoleGroupMap);
            data.put(WebCode.SUCCESS, true);
            data.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            data.put(WebCode.SUCCESS, false);
            data.put(WebCode.MSG, WebCode.FAILURE_SAVE);
        }
        return data;
    }


    /**
     * 分页查询角色授权数据
     *
     * @param page
     * @param model
     * @param dashLayoutRoleMap
     * @return
     */
    @ApiOperation("分页查询角色授权数据")
    @RequestMapping(value = "/searchDashLayoutRoleMap")
    public Map<String, Object> searchDashLayoutRoleMap(Page<DashLayoutRoleMap> page, Model model, DashLayoutRoleMap dashLayoutRoleMap) {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<DashLayoutRoleMap> pages = dashLayoutRoleMapService.searchDashLayoutRoleMap(page, dashLayoutRoleMap);
        map.put(RESULT_TOTAL, pages.getTotal());
        map.put(RESULT_ROWS, pages.getResult());
        return map;
    }


    /**
     * 角色授权数据保存
     *
     * @param dashLayoutRoleMap
     * @param model
     * @return
     */
    @ApiOperation("角色授权数据保存")
    @PostMapping("/roleLayoutMapSave")
    public Map<String, Object> roleLayoutMapSave(@RequestBody DashLayoutRoleMap dashLayoutRoleMap, Model model) {
        Map<String, Object> data = new HashMap<String, Object>();
        try {
            dashLayoutRoleMapService.saveLayoutRoleMap(dashLayoutRoleMap);
            data.put(WebCode.SUCCESS, true);
            data.put(WebCode.MSG, WebCode.SUCCESS_SAVE);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage(), e);
            data.put(WebCode.SUCCESS, false);
            data.put(WebCode.MSG, WebCode.FAILURE_SAVE);
        }
        return data;
    }
}